package cn.ggsaas.base.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import cn.ggsaas.base.exception.BasicException;

import java.util.Date;

/**
 * token的工具类
 *
 * @author xuming
 * @date 2020-06-23
 */
public class JWTUtil {

    /**
     * 毫秒 小时数*分钟数*秒数*毫秒
     */
    public static final long TOKEN_EXPIRE_TIME = 8 * 60 * 60 * 1000;

    /**
     * secretKey 加密密钥
     */
    public static final String SECRET_KEY = "dog@ui";

    /**
     * 生成Token
     * @param userId 用户id
     * @param email 用户邮箱
     * @param phone 手机
     * @param name 用户真实姓名
     * @return 新生成的Token
     */
    public static String generateToken(Long userId, String email, String phone, String name) {
        Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
        Date now = new Date();
        Date expireTime = new Date(now.getTime() + TOKEN_EXPIRE_TIME);

        String token = JWT.create()
                //.withIssuer(ISSUER)
                .withIssuedAt(now)
                .withExpiresAt(expireTime)
                .withClaim("userId", userId)
                .withClaim("email", email)
                .withClaim("phone", phone)
                .withClaim("name", name)
                .sign(algorithm);

        return token;
    }


    /**
     * 校验Token
     *
     * @param token token
     * @param secretKey 加密密钥
     */
    public static void verifyToken(String token, String secretKey) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(secretKey);
            JWTVerifier jwtVerifier = JWT.require(algorithm).build();
            jwtVerifier.verify(token);
        } catch (Exception e) {
            throw new BasicException("token失效");
        }
    }

    /**
     * 从Token中提取 用户id
     *
     * @param token token
     * @return 用户id
     */
    public static Long getUserId(String token) {
        return JWT.decode(token).getClaim("userId").asLong();
    }

    /**
     * 从Token中提取 用户邮箱
     *
     * @param token token
     * @return 用户邮箱
     */
    public static String getUserEmail(String token) {
        return JWT.decode(token).getClaim("email").asString();
    }

    /**
     * 从Token中提取 用户手机
     *
     * @param token token
     * @return 用户手机
     */
    public static String getUserPhone(String token) {
        return JWT.decode(token).getClaim("phone").asString();
    }

    /**
     * 从Token中提 用户姓名
     *
     * @param token token
     * @return 用户姓名
     */
    public static String getUserName(String token) {
        return JWT.decode(token).getClaim("name").asString();
    }

    public static void main(String[] args) {

        //JWTUtil.verifyToken(JWTUtil.generateToken(20203L, "123456"), "123456");
        //String token = JWTUtil.generateToken(20203L, "123456@qq.com", "18888888888", "欧阳八天", "ym@123456");
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjE4ODg4ODg4ODg4IiwibmFtZSI6Iuasp-mYs-WFq-WkqSIsImV4cCI6MTU5ODgwOTQ4NiwiaWF0IjoxNTk4NzgwNjg2LCJ1c2VySWQiOjIwMjAzLCJlbWFpbCI6IjEyMzQ1NkBxcS5jb20ifQ.qiwYcuoIMMpfFr5KYCGpSs6gQCkISgi7wECTBpn1MyI";
        System.out.println(token);
        System.out.println(JWTUtil.getUserEmail("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjE4ODg4ODg4ODg4IiwibmFtZSI6Iuasp-mYs-WFq-WkqSIsImV4cCI6MTU5ODgwOTQ4NiwiaWF0IjoxNTk4NzgwNjg2LCJ1c2VySWQiOjIwMjAzLCJlbWFpbCI6IjEyMzQ1NkBxcS5jb20ifQ.qiwYcuoIMMpfFr5KYCGpSs6gQCkISgi7wECTBpn1MyI"));
        JWTUtil.verifyToken(token, "yM@123456");
    }


}